package interrupt;


import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;


@SuppressWarnings("serial")
public class GUI {
    private static TEC tec;
    private final String MatchOrder = "^[ ]*[A|E|U|G][ ]+[0-9A-F]{1,4}[ ]*$";
    private static ArrayList<String> Records;
    private String Mode;
    private static Thread bind;
    private boolean front_running = false;
    private boolean back_running = false;
    private static StringBuffer CurrentText;
    private LinkedList<Short> keyTypes = new LinkedList<Short>();
    private JPanel jPanel;
    public GUI() {
        CurrentText = new StringBuffer("");
        initComponents();
        Records = new ArrayList<String>();
        tec = new TEC();
        this.Refresh();
        this.Mode = "Order";
    }

    public JPanel getjPanel(){
        return jPanel;
    }

    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel = new JPanel();

        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        Reset = new javax.swing.JButton();
        Start = new javax.swing.JButton();
        Button1 = new javax.swing.JButton();
        Button2 = new javax.swing.JButton();
        Button0 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        R0_Value = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        R1_Value = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        R2_Value = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        R3_Value = new javax.swing.JTextField();
        R6_Value = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        R7_Value = new javax.swing.JTextField();
        SP_Value = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        PC_Value = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        R10_Value = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        R11_Value = new javax.swing.JTextField();
        R8_Value = new javax.swing.JTextField();
        R14_Value = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        R9_Value = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        R15_Value = new javax.swing.JTextField();
        R12_Value = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        R13_Value = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();

//        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jPanel.setMaximumSize(new java.awt.Dimension(1366, 768));
        jPanel.setMinimumSize(new java.awt.Dimension(1024, 600));
//        setName("中断实验演示"); // NOI18N
        jPanel.setPreferredSize(new java.awt.Dimension(1074, 700));
//        setResizable(false);

        output.setEditable(false);
        output.setBackground(new java.awt.Color(0, 0, 0));
        output.setColumns(20);
        output.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        output.setForeground(new java.awt.Color(255, 255, 255));
        output.setRows(5);
        output.setSelectedTextColor(new java.awt.Color(0, 0, 0));
        output.setSelectionColor(new java.awt.Color(0, 0, 0));
        output.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                outputKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                outputKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(output);

        Reset.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        Reset.setText(" Reset ");
        Reset.setMaximumSize(new java.awt.Dimension(88, 27));
        Reset.setMinimumSize(new java.awt.Dimension(88, 27));
        Reset.setPreferredSize(new java.awt.Dimension(88, 27));
        Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	CurrentText.setLength(0);
                ResetActionPerformed(evt);
            }
        });

        Start.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        Start.setText(" Start ");
        Start.setInheritsPopupMenu(true);
        Start.setMaximumSize(new java.awt.Dimension(88, 27));
        Start.setMinimumSize(new java.awt.Dimension(88, 27));
        Start.setPreferredSize(new java.awt.Dimension(88, 27));
        Start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartActionPerformed(evt);
            }
        });

        Button1.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        Button1.setText("Button1");
        Button1.setMaximumSize(new java.awt.Dimension(88, 27));
        Button1.setMinimumSize(new java.awt.Dimension(88, 27));
        Button1.setPreferredSize(new java.awt.Dimension(88, 27));
        Button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button1ActionPerformed(evt);
            }
        });

        Button2.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        Button2.setText("Button2");
        Button2.setMaximumSize(new java.awt.Dimension(88, 27));
        Button2.setMinimumSize(new java.awt.Dimension(88, 27));
        Button2.setName(""); // NOI18N
        Button2.setPreferredSize(new java.awt.Dimension(88, 27));
        Button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button2ActionPerformed(evt);
            }
        });

        Button0.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        Button0.setText("Button0");
        Button0.setMaximumSize(new java.awt.Dimension(88, 27));
        Button0.setMinimumSize(new java.awt.Dimension(88, 27));
        Button0.setPreferredSize(new java.awt.Dimension(88, 27));
        Button0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button0ActionPerformed(evt);
            }
        });

        

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(Reset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Reset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Start, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Button2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Button0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel1.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel1.setText("R0 ");

        R0_Value.setEditable(false);
        R0_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R0_Value.setText("0000");

        jLabel2.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel2.setText("R1 ");

        R1_Value.setEditable(false);
        R1_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R1_Value.setText("0000");

        jLabel3.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel3.setText("R2 ");

        R2_Value.setEditable(false);
        R2_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R2_Value.setText("0000");

        jLabel4.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel4.setText("R3 ");

        R3_Value.setEditable(false);
        R3_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R3_Value.setText("0000");

        R6_Value.setEditable(false);
        R6_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R6_Value.setText("0000");

        jLabel5.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel5.setText("R7 ");

        jLabel6.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel6.setText("SP ");

        R7_Value.setEditable(false);
        R7_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R7_Value.setText("0000");

        SP_Value.setEditable(false);
        SP_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        SP_Value.setText("0000");

        jLabel7.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel7.setText("PC ");

        PC_Value.setEditable(false);
        PC_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        PC_Value.setText("0000");

        jLabel8.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel8.setText("R6 ");

        R10_Value.setEditable(false);
        R10_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R10_Value.setText("0000");

        jLabel9.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel9.setText("R11");

        jLabel10.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel10.setText("R8 ");

        R11_Value.setEditable(false);
        R11_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R11_Value.setText("0000");

        R8_Value.setEditable(false);
        R8_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R8_Value.setText("0000");

        R14_Value.setEditable(false);
        R14_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R14_Value.setText("0000");

        jLabel11.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel11.setText("R9 ");

        jLabel12.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel12.setText("R15");

        R9_Value.setEditable(false);
        R9_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R9_Value.setText("0000");

        jLabel13.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel13.setText("R12");

        R15_Value.setEditable(false);
        R15_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R15_Value.setText("0000");

        R12_Value.setEditable(false);
        R12_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R12_Value.setText("0000");

        jLabel14.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel14.setText("R13");

        R13_Value.setEditable(false);
        R13_Value.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        R13_Value.setText("0000");

        jLabel15.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel15.setText("R10");

        jLabel16.setFont(new java.awt.Font("Consolas", 1, 15)); // NOI18N
        jLabel16.setText("R14");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R0_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R8_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R1_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R2_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R3_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(SP_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(PC_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R6_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R7_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R9_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel15)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R10_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R11_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel13)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R12_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel14)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R13_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel16)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R14_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel12)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(R15_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(R0_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(R1_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(R2_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(R3_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(SP_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(PC_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(R6_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(R7_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(R8_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(R9_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(R10_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(R11_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(R12_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(R13_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16)
                    .addComponent(R14_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(R15_Value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel);
        jPanel.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 574, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(80, Short.MAX_VALUE))
        );
//
//        pack();
//        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetActionPerformed
        this.front_running = false;
        this.back_running = false;
        this.Mode = "Order";
        output.requestFocus();
    }//GEN-LAST:event_ResetActionPerformed

    private void StartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartActionPerformed
        this.front_running = true;
        tec.init();
        Records.clear();
        Records.add("TEC-XP Simulation Program");
        Records.add("Version 2.0 2016.1");
        Records.add("Computer Architectur DUT");
        Records.add("Copyright Andy Liu");
        Records.add(">");
        tec.test();
        output.requestFocus();
    }//GEN-LAST:event_StartActionPerformed

    private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button2ActionPerformed
        Thread i = new Thread(){
            @Override
            public void run(){
                output.requestFocus();
                tec.interrupts.push((byte)2);
            }
        };
        i.start();
    }//GEN-LAST:event_Button2ActionPerformed

    private void outputKeyReleased(final java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputKeyReleased
        if(!front_running)
            return;
        bind = new Thread(){
            @Override
            public void run(){
                int keyCode = evt.getKeyCode();
                if(keyCode == 10) {
                    try {
                        analyze();
                    } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InterruptedException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if( keyCode == 8){
                    if(CurrentText.length() > 0) {
                        CurrentText.setLength(CurrentText.length()-1);
                    }
                } else if (keyCode == 44 || keyCode == 32 || 
                        (keyCode >= 48 && keyCode <= 57) || 
                        (keyCode >= 65 && keyCode <= 90) || 
                        (keyCode >= 97 && keyCode <= 122) ){
                        if(!back_running)
                            CurrentText.append((char) keyCode);
                }
            }
        };
        bind.start();
    }//GEN-LAST:event_outputKeyReleased

    private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button1ActionPerformed
        Thread i = new Thread(){
            @Override
            public void run(){
                output.requestFocus();
                tec.interrupts.push((byte)1);
            }
        };
        i.start();
    }//GEN-LAST:event_Button1ActionPerformed

    private void Button0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button0ActionPerformed
        Thread i = new Thread(){
            @Override
            public void run(){
                output.requestFocus();
                tec.interrupts.push((byte)0);
            }
        };
        i.start();
    }//GEN-LAST:event_Button0ActionPerformed

    private void outputKeyTyped(final java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputKeyTyped
        
        Thread type = new Thread(){
            @Override
            public void run(){
                if(keyTypes.size() <= 3)
                    keyTypes.addLast((short) evt.getKeyCode());
            }
        };
        if(tec.getSignIn() == true  && back_running){
            type.start();
        }
    }//GEN-LAST:event_outputKeyTyped

    //private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    //    tec.test();        // TODO add your handling code here:
    //}//GEN-LAST:event_jButton1ActionPerformed

    private void analyze() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InterruptedException {
        String text = CurrentText.toString();
        CurrentText.setLength(0);
        final String[] texts;
        
        Records.set(Records.size()-1, Records.get(Records.size()-1) + text);
        if((Mode.equals("Order") && !text.matches(MatchOrder)) || (text.isEmpty())) {
            if(!text.isEmpty())     {
                Records.add("          ^Error!");
            } else {
                Mode = "Order";
            }
            if(Mode.equals("Order")){
                Records.add(">");
            } else {
                Records.add(Tools.fill(Integer.toHexString(tec.getPC())) + ": ");
            }
            CurrentText.setLength(0);
            return;
        }
        texts = text.split(",| ");
        
        
        Thread main = new Thread(){
            @Override
            public void run(){
                try {
                    System.out.println("Start");
                    back_running = true;
                    tec.init();
                    tec.setPC((short) Integer.parseInt(texts[1], 16));
                    Records.add("");
                    
                    String[] result = tec.run();
                    R0_Value.setText(result[0]);
                    R1_Value.setText(result[1]);
                    R2_Value.setText(result[2]);
                    R3_Value.setText(result[3]);
                    SP_Value.setText(result[4]);
                    PC_Value.setText(result[5]);
                    R6_Value.setText(result[6]);
                    R7_Value.setText(result[7]);
                    R8_Value.setText(result[8]);
                    R9_Value.setText(result[9]);
                    R10_Value.setText(result[10]);
                    R11_Value.setText(result[11]);
                    R12_Value.setText(result[12]);
                    R13_Value.setText(result[13]);
                    R14_Value.setText(result[14]);
                    R15_Value.setText(result[15]);
                    Records.add(">");
                    System.out.println("\n");
                    back_running = false;
                } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InterruptedException ex) {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
                
        if(Mode.equals("Order")) {
            switch (texts[0]) {
                case "G":
                    main.start();
                    break;
                case "A":
                case "E":
                    Mode = "Mode" + texts[0];
                    tec.setPC((short) Integer.parseInt(texts[1], 16));
                    Records.add(Tools.fill(Integer.toHexString(tec.getPC())) + ": ");
                    break;
                case "T":
                    break;
                case "U":
                    String[] orders = tec.getOrders(texts[1]).split("\n");
                    Records.addAll(Arrays.asList(orders));
                    Records.add(">");
                    break;
                default:
                    break;
            }
        }
        else {
            if(!tec.Input(text, Mode)){
                StringBuilder error = new StringBuilder("      ");
                for(int i = 0; i < text.length(); i++){
                    error.append(" ");
                }
                error.append("^Error");
                Records.add(error.toString());
            }
            Records.add(Tools.fill(Integer.toHexString(tec.getPC())) + ": ");
        }
    }
    
    private void Refresh(){
        Thread r = new Thread(){
            @Override
            public void run(){
                while(true) {
                    try {
                        if(!Records.isEmpty()){
                            output.setText("");
                            for(int i = 0; i < Records.size()-1; i++){
                                output.append(Records.get(i) + "\n");
                            }
                            output.append(Records.get(Records.size()-1) + CurrentText);
                        }
                        
                        if(tec.getSignOut()) {
                            String last = Records.get(Records.size()-1);
                            if(last.length() < 100)
                                Records.set(Records.size()-1, last + tec.getIOPORT());
                            else
                                Records.add(tec.getIOPORT());
                        }
                        if(!keyTypes.isEmpty()){
                            tec.setIOPORT((Short)keyTypes.removeFirst());
                        }
                        while(Records.size() > 29){
                            Records.remove(0);
                        }
                        sleep(10);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        };
        r.start();
    }

//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
//            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        //</editor-fold>
//
//        /* Create and display the form */
//            new GUI().setVisible(true);
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Button0;
    private javax.swing.JButton Button1;
    private javax.swing.JButton Button2;
    private javax.swing.JTextField PC_Value;
    private javax.swing.JTextField R0_Value;
    private javax.swing.JTextField R10_Value;
    private javax.swing.JTextField R11_Value;
    private javax.swing.JTextField R12_Value;
    private javax.swing.JTextField R13_Value;
    private javax.swing.JTextField R14_Value;
    private javax.swing.JTextField R15_Value;
    private javax.swing.JTextField R1_Value;
    private javax.swing.JTextField R2_Value;
    private javax.swing.JTextField R3_Value;
    private javax.swing.JTextField R6_Value;
    private javax.swing.JTextField R7_Value;
    private javax.swing.JTextField R8_Value;
    private javax.swing.JTextField R9_Value;
    private javax.swing.JButton Reset;
    private javax.swing.JTextField SP_Value;
    private javax.swing.JButton Start;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea output;
    // End of variables declaration//GEN-END:variables
}
